package com.itheima.homeweek;

import java.io.*;

public class HomeWeek02 {/*键盘录入数据源路径和目的地路径，分别使用【普通字符流】和【字符缓冲流的特殊功能】完成对指定文件的复制
    例如：
        数据源： d:\a.txt
        目的地： d:\b.txt*/
    public static void main(String[] args) throws IOException {
        File src = new File("day017\\HomeWeek01.txt");
        File goal = new File("day017\\HomeWeek03.txt");
        FileWriter fw = new FileWriter(goal);
        FileReader fr = new FileReader(src);
        //copyFile1(src,goal);
        copyFile2(fr,fw);
    }
    //普通字符流完成复制
    private static void copyFile1(File src,File goal) throws IOException {
        //创建字符流对象
        FileWriter fw = new FileWriter(goal);
        FileReader fr = new FileReader(src);
        //复制数据
        int len;
        char[] chs = new char[1024];
        while( (len=fr.read(chs))!=-1){
            fw.write(chs,0,len);
        }
        //释放资源
        fw.close();
        fr.close();
    }
    //字符缓冲流复制
    private static void copyFile2(FileReader fw,FileWriter fr) throws IOException {
        //创建对象
        BufferedWriter bw =  new BufferedWriter(fr);
        BufferedReader br = new BufferedReader(fw);
        String s;
        while(( s= br.readLine())!= null){
            bw.write(s);
            bw.newLine();
        }
        //释放资源
        bw.close();
        br.close();
    }
}
